package com.hzmg.route.akka.route.sample1;

import akka.actor.typed.*;
import akka.actor.typed.javadsl.Behaviors;
import akka.actor.typed.javadsl.PoolRouter;
import akka.actor.typed.javadsl.Routers;

public class PoolRoute {
    static Behavior<Void> showPoolRouting() {
        return
                Behaviors.setup(
                        context -> {
                            int poolSize = 4;
                            PoolRouter<Worker.Command> pool =
                                    Routers.pool(
                                            poolSize,
                                            Behaviors.supervise(Worker.create()).onFailure(SupervisorStrategy.restart()));
                            ActorRef<Worker.Command> router = context.spawn(pool, "worker-pool");

                            for (int i = 0; i < 10; i++) {
                                router.tell(new Worker.DoLog("msg" + i));
                            }

                            PoolRouter<Worker.Command> blockingPool =
                                    pool.withRouteeProps(DispatcherSelector.blocking());

                            ActorRef<Worker.Command> blockingRouter =
                                    context.spawn(blockingPool, "blocking-pool", DispatcherSelector.sameAsParent());

                            // 路由策略（轮询）
                            PoolRouter<Worker.Command> alternativePool = pool.withPoolSize(2).withRoundRobinRouting();

                            PoolRouter<Worker.Command> broadcastingPool =
                                    pool.withBroadcastPredicate(msg -> msg instanceof DoBroadcastLog);

                            return Behaviors.empty();

                        });
    }


    public static void main(String[] args) {
        ActorSystem<Void> system =
                ActorSystem.create(
                        Behaviors.setup(
                                context -> {
                                    context.spawn(showPoolRouting(), "pool-router-setup");
                                    return Behaviors.empty();
                                }),
                        "RouterTest");

    }
}
